package com.bwie.conferenceroom.fafka;

import com.alibaba.fastjson.JSONObject;
import com.bwie.common.domain.request.ConferenceRoomReq;
import com.bwie.common.domain.response.ConferenceRoomRes;
import com.bwie.conferenceroom.service.ConferenceRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

@Component
public class Consumer {
    @Autowired
    private ConferenceRoomService conferenceRoomService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 异步方法同步缓存
     * @param conferenceRoomReq
     */
    @KafkaListener(topics = "updConferenceRoom")
    public void updConferenceRoom(ConferenceRoomReq conferenceRoomReq){
        redisTemplate.delete("ConferenceRoomResList");
        List<ConferenceRoomRes> conferenceRoomList = conferenceRoomService.getConferenceRoomList(conferenceRoomReq);
        if (!CollectionUtils.isEmpty(conferenceRoomList)){
            redisTemplate.opsForValue().set("ConferenceRoomResList", JSONObject.toJSONString(conferenceRoomList));
        }
    }
}
